//// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
//package com.linqingying.gitcode.pullrequest.ui.list
//
//import com.intellij.collaboration.ui.codereview.avatar.Avatar
//import com.intellij.collaboration.ui.codereview.avatar.CodeReviewAvatarUtils
//import com.intellij.collaboration.ui.codereview.details.ReviewDetailsUIUtil
//import com.intellij.collaboration.ui.codereview.details.data.ReviewState
//import com.intellij.collaboration.ui.codereview.list.*
//import com.intellij.ide.DataManager
//import com.intellij.openapi.actionSystem.*
//import com.intellij.openapi.actionSystem.ex.ActionUtil
//import com.intellij.openapi.util.NlsSafe
//import com.intellij.ui.ColorHexUtil
//import com.intellij.ui.PopupHandler
//import com.intellij.ui.components.JBList
//import icons.CollaborationToolsIcons
//import com.linqingying.gitcode.api.data.GCActor
//import com.linqingying.gitcode.api.data.GCLabel
//import com.linqingying.gitcode.api.data.GCUser
//import com.linqingying.gitcode.api.data.pullrequest.*
//import com.linqingying.gitcode.i18n.GitCodeBundle
//import com.linqingying.gitcode.pullrequest.action.GCPRActionKeys
//import com.linqingying.gitcode.pullrequest.data.service.GCPRPersistentInteractionState
//import com.linqingying.gitcode.pullrequest.ui.GCReviewersUtils
//import com.linqingying.gitcode.ui.avatars.GCAvatarIconsProvider
//import com.linqingying.gitcode.ui.util.GCUIUtil
//import javax.swing.ListModel
//
//internal class GCPRListComponentFactory(
//  private val interactionStateService: GCPRPersistentInteractionState,
//  private val listModel: ListModel<GCPullRequestShort>
//) {
//
//  fun create(avatarIconsProvider: GCAvatarIconsProvider, ghostUser: GCUser, currentUser: GCUser): JBList<GCPullRequestShort> {
//    return ReviewListComponentFactory(listModel).create {
//      presentPR(avatarIconsProvider, it, ghostUser, currentUser)
//    }.also {
//      DataManager.registerDataProvider(it) { dataId ->
//        when {
//          GCPRActionKeys.PULL_REQUEST_ID.`is`(dataId) -> it.selectedValue?.prId
//          GCPRActionKeys.PULL_REQUEST_URL.`is`(dataId) -> it.selectedValue?.url
//          else -> null
//        }
//      }
//      val actionGroup = ActionManager.getInstance().getAction("GitCode.PullRequest.ToolWindow.List.Popup") as ActionGroup
//      PopupHandler.installPopupMenu(it, actionGroup, ActionPlaces.POPUP)
//      val shortcuts = CompositeShortcutSet(CommonShortcuts.ENTER, CommonShortcuts.DOUBLE_CLICK_1)
//      ActionUtil.wrap("GitCode.PullRequest.Show").registerCustomShortcutSet(shortcuts, it)
//    }
//  }
//
//  private fun presentPR(
//    avatarIconsProvider: GCAvatarIconsProvider,
//    pr: GCPullRequestShort,
//    ghostUser: GCUser,
//    currentUser: GCUser
//  ): ReviewListItemPresentation =
//    ReviewListItemPresentation.Simple(pr.title, "#" + pr.number, pr.createdAt,
//                                      author = createUserPresentation(avatarIconsProvider, pr.author),
//                                      tagGroup = NamedCollection.create(GitCodeBundle.message("pull.request.labels.popup", pr.labels.size),
//                                                                        pr.labels.map(::getLabelPresentation)),
//                                      mergeableStatus = getMergeableStatus(pr.mergeable),
//                                      state = getStateText(pr.state, pr.isDraft),
//                                      userGroup1 = getAssigneesPresentation(avatarIconsProvider, pr.assignees),
//                                      userGroup2 = getReviewersPresentation(
//                                        avatarIconsProvider,
//                                        GCReviewersUtils.getReviewsByReviewers(
//                                          pr.author,
//                                          pr.reviews,
//                                          pr.reviewRequests.mapNotNull(GCPullRequestReviewRequest::requestedReviewer),
//                                          ghostUser
//                                        )
//                                      ),
//                                      commentsCounter = ReviewListItemPresentation.CommentsCounter(
//                                        pr.unresolvedReviewThreadsCount,
//                                        GitCodeBundle.message("pull.request.unresolved.comments", pr.unresolvedReviewThreadsCount)
//                                      ),
//                                      seen = interactionStateService.isSeen(pr, currentUser))
//
//  private fun getLabelPresentation(label: GCLabel) =
//    TagPresentation.Simple(label.name, ColorHexUtil.fromHex(label.color))
//
//  private fun getStateText(state: GCPullRequestState, isDraft: Boolean): @NlsSafe String? {
//    if (state == GCPullRequestState.OPEN && !isDraft) return null
//    return GCUIUtil.getPullRequestStateText(state, isDraft)
//  }
//
//  private fun getMergeableStatus(mergeableState: GCPullRequestMergeableState): ReviewListItemPresentation.Status? {
//    if (mergeableState == GCPullRequestMergeableState.CONFLICTING) {
//      return ReviewListItemPresentation.Status(CollaborationToolsIcons.Review.NonMergeable,
//                                               GitCodeBundle.message("pull.request.conflicts.merge.tooltip"))
//    }
//
//    return null
//  }
//
//  private fun getAssigneesPresentation(avatarIconsProvider: GCAvatarIconsProvider,
//                                       assignees: List<GCUser>): NamedCollection<UserPresentation>? {
//    return NamedCollection.create(GitCodeBundle.message("pull.request.assignees.popup", assignees.size),
//                                  assignees.map { user -> createUserPresentation(avatarIconsProvider, user, null) })
//  }
//
//  private fun getReviewersPresentation(
//    avatarIconsProvider: GCAvatarIconsProvider,
//    reviewsByReviewers: Map<GCPullRequestRequestedReviewer, ReviewState>
//  ): NamedCollection<UserPresentation>? {
//    val presentations = createUserPresentationByFilter(avatarIconsProvider, reviewsByReviewers, ReviewState.ACCEPTED) +
//                        createUserPresentationByFilter(avatarIconsProvider, reviewsByReviewers, ReviewState.WAIT_FOR_UPDATES) +
//                        createUserPresentationByFilter(avatarIconsProvider, reviewsByReviewers, ReviewState.NEED_REVIEW)
//
//    return NamedCollection.create(GitCodeBundle.message("pull.request.reviewers.popup", presentations.size), presentations)
//  }
//
//  private fun createUserPresentation(avatarIconsProvider: GCAvatarIconsProvider, user: GCActor?): UserPresentation? {
//    if (user == null) return null
//    return UserPresentation.Simple(user.login, null, avatarIconsProvider.getIcon(user.avatarUrl, Avatar.Sizes.BASE))
//  }
//
//  private fun createUserPresentationByFilter(
//    avatarIconsProvider: GCAvatarIconsProvider,
//    reviewsByReviewers: Map<GCPullRequestRequestedReviewer, ReviewState>,
//    reviewStateFilter: ReviewState
//  ): List<UserPresentation> {
//    return reviewsByReviewers
//      .filterValues { reviewState -> reviewState == reviewStateFilter }
//      .keys
//      .map { reviewer -> createUserPresentation(avatarIconsProvider, reviewer, reviewStateFilter) }
//  }
//
//  private fun createUserPresentation(
//    avatarIconsProvider: GCAvatarIconsProvider,
//    user: GCPullRequestRequestedReviewer,
//    reviewState: ReviewState?
//  ): UserPresentation {
//    val avatarIcon = avatarIconsProvider.getIcon(user.avatarUrl, Avatar.Sizes.BASE)
//    val icon = if (reviewState != null) {
//      val outlineColor = ReviewDetailsUIUtil.getReviewStateIconBorder(reviewState)
//      CodeReviewAvatarUtils.outlinedAvatarIcon(avatarIcon, outlineColor)
//    }
//    else {
//      avatarIcon
//    }
//
//    return UserPresentation.Simple(user.shortName, user.name, icon)
//  }
//}
